﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MemoryVerse;

namespace WFWorshipManager
{
    public partial class QuizMeMain : Form
    {
        public QuizMeMain()
        {
            InitializeComponent();
            bookDataGridViewTextBoxColumn.DataSource = Enum.GetValues(typeof(MemoryVerse.BibleBooks));
            QuestionTimer.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var VL = new MemoryVerse.VerseLibrary();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow && !row.ReadOnly)
                {
                    var verse = new Verse(   
                    (BibleBooks)Enum.Parse(typeof(BibleBooks), row.Cells[0].Value.ToString())
                    ,int.Parse(row.Cells[1].Value.ToString())
                    ,int.Parse(row.Cells[3].Value.ToString())
                    ,int.Parse(row.Cells[2].Value.ToString())
                    ,row.Cells[4].Value.ToString()
                    );

                    VL.Add(verse);
                }
            }
            LibraryManager.SaveLibrary(VL);
        }

        private void verseLibraryBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            VerseLibrary VL = LibraryManager.LoadLibrary();
            dataGridView1.DataSource = VL.VerseList;
            dataGridView1.Refresh();
        }

        private void verseBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
        }

        private void QuizMeMain_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void niQuizMe_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;

        }

        private void QuestionTimer_Tick(object sender, EventArgs e)
        {
            Quiz UForm = null;
          UForm = (Quiz)IsFormAlreadyOpen(typeof(Quiz));
          if (UForm == null)
            {
                UForm = new Quiz();
                UForm.Show();
            }
        }

        public static Form IsFormAlreadyOpen(Type FormType)
        {
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return OpenForm;
            }

            return null;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Quiz UForm;
            if (dataGridView1.SelectedRows.Count == 1)
            {
                UForm = new Quiz(dataGridView1.SelectedRows[0].Index);
            }
            else
            {
                UForm = new Quiz();
            }
            UForm.Show();
        }

    }
}
